package com.fsy.fmms.workorder.controller;

import cc.mrbird.febs.common.annotation.Log;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.domain.QueryRequest;
import cc.mrbird.febs.common.exception.FebsException;
import cc.mrbird.febs.common.service.CacheService;
import com.fsy.fmms.workorder.domain.ProcessHistory;
import com.fsy.fmms.workorder.domain.WorkOrder;
import com.fsy.fmms.workorder.service.WorkOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Slf4j
@Validated
@RestController
@RequestMapping("workorder")
@Api(description = "任务工单管理接口")
public class WorkOrderController extends BaseController {

    @Autowired
    private WorkOrderService workOrderService;

    @Autowired
    private CacheService cacheService;

    @ApiOperation(value="创建工单", notes = "详细描述")
    @Log("创建任务工单")
    @PostMapping("add")
    public void add(@Valid WorkOrder order) {
        log.info("add workOrder: {}", order);
        try {
            workOrderService.add(order);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value="查询任务工单", notes="详细描述")
    @GetMapping("list")
    public Map<String, Object> list(QueryRequest queryRequest, WorkOrder order) {
        return getDataTable(workOrderService.findWorkOrders(order, queryRequest));
    }

    @ApiOperation(value="处理任务工单", notes="详细描述")
    @Log("处理任务工单")
    @PostMapping("handle")
    public void handle(WorkOrder order) throws FebsException {
        try {
            workOrderService.handle(order);
        } catch (Exception e) {
            String message = "处理任务工单失败：" + e.getMessage();
            log.error("处理任务工单失败：", e);
            throw new FebsException(message);
        }
    }

    @ApiOperation(value="获取任务工单处理记录", notes="详细描述")
    @ApiImplicitParam(name="workOrderId", value="任务工单号", required = true)
    @GetMapping("processHistory")
    public List<ProcessHistory> processHistory(Integer workOrderId) {
        return workOrderService.getProcessHistory(workOrderId);
    }
}
